/*
 * Copyright 2018-present KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.uduew.sjd.ui.base

import android.content.Intent
import android.content.res.Resources
import android.graphics.Color
import android.os.Bundle
import android.view.inputmethod.InputMethodManager
import androidx.lifecycle.ViewModel
import com.uduew.sjd.ui.web.WebViewActivity
import com.uduew.sjd.utils.NetworkStateManager
import com.uduew.sjd.utils.StatusBarUtil
import com.kunminx.architecture.ui.page.DataBindingActivity
import com.kunminx.architecture.ui.scope.ViewModelScope
import androidx.core.net.toUri
import com.uduew.utils.AdaptScreenUtils
import com.uduew.utils.BarUtils
import com.uduew.utils.ScreenUtils

/**
 * Create by KunMinX at 19/8/1
 */
abstract class BaseActivity : DataBindingActivity() {
    private val mViewModelScope = ViewModelScope()

    override fun onCreate(savedInstanceState: Bundle?) {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
//        BarUtils.setStatusBarLightMode(this, true)
        StatusBarUtil.setStatusBar(this, false, 0, true)

        if (supportActionBar != null) {
            supportActionBar?.hide()
        }

        super.onCreate(savedInstanceState)

        lifecycle.addObserver(NetworkStateManager.getInstance())

        //TODO tip 1: DataBinding 严格模式（详见 DataBindingActivity - - - - - ）：
        // 将 DataBinding 实例限制于 base 页面中，默认不向子类暴露，
        // 通过这方式，彻底解决 View 实例 Null 安全一致性问题，
        // 如此，View 实例 Null 安全性将和基于函数式编程思想的 Jetpack Compose 持平。

        // 如这么说无体会，详见 https://xiaozhuanlan.com/topic/9816742350 和 https://xiaozhuanlan.com/topic/2356748910
    }

    //TODO tip 2: Jetpack 通过 "工厂模式" 实现 ViewModel 作用域可控，
    //目前我们在项目中提供了 Application、Activity、Fragment 三个级别的作用域，
    //值得注意的是，通过不同作用域 Provider 获得 ViewModel 实例非同一个，
    //故若 ViewModel 状态信息保留不符合预期，可从该角度出发排查 是否眼前 ViewModel 实例非目标实例所致。
    //如这么说无体会，详见 https://xiaozhuanlan.com/topic/6257931840
    protected fun <T : ViewModel?> getActivityScopeViewModel(modelClass: Class<T>): T {
        return mViewModelScope.getActivityScopeViewModel(this, modelClass)
    }

    protected fun <T : ViewModel?> getApplicationScopeViewModel(modelClass: Class<T>): T {
        return mViewModelScope.getApplicationScopeViewModel(modelClass)
    }

    override fun getResources(): Resources {
        return if (ScreenUtils.isPortrait()) {
            AdaptScreenUtils.adaptWidth(super.getResources(), 360)
        } else {
            AdaptScreenUtils.adaptHeight(super.getResources(), 640)
        }
    }

    protected fun toggleSoftInput() {
        val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
    }

    protected fun openUrlInBrowser(url: String?) {
        val uri = url?.toUri()
        val intent = Intent(Intent.ACTION_VIEW, uri)
        startActivity(intent)
    }

    protected fun openUrlInWebView(url: String?, title: String?) {
        val intent = Intent(this, WebViewActivity::class.java)
        intent.putExtra("URL", url)
        intent.putExtra("TITLE", title)
        startActivity(intent)
    }
}
